#ifndef _LOG_H_
#define _LOG_H_

#define LOG_ENABLE_ALL

#include <list>
#include "singleton.h"
#include "file.h"
#include "debug.h"
#include <stdlib.h>
#include <windows.h>

enum log_type
{
	log_fatal,
	log_error,
	log_debug
};

class log_writer
{
public:
	virtual ~log_writer(){}
	virtual void write(const char* _string)=0;
};

class logger: public singleton<logger>
{
public:
	logger();
	~logger();
	void write(const char* _string,...);
	void bind_writer(log_writer* _writer);
private:
	std::list<log_writer*> m_writers;
	char m_buffer[1024];
};

class log_file: public log_writer
{
public:
	log_file(const char* _fname);
	~log_file();

	void write(const char* _string);
private:
	file m_file;
};

#define LOG_DEBUG_INLINE(...) 

#define LOG_ERROR_INLINE(...) {\
								char tmp[1024];\
								sprintf_s(tmp,__VA_ARGS__);\
								char buf[1024];\
								sprintf_s(buf,"%s\nExit program?",tmp);\
								if(MessageBox(GetActiveWindow(),buf,"nEngine error",MB_OKCANCEL|MB_ICONWARNING|MB_TOPMOST)==1) throw tmp;\
							  }

#define LOG_FATAL_INLINE(...) {\
								char buf[1024];\
								sprintf_s(buf,__VA_ARGS__);\
								throw buf;\
							  }

#ifdef  LOG_ENABLE_ALL
#define LOG_FATAL  LOG_FATAL_INLINE
#define LOG_ERROR  LOG_ERROR_INLINE
#define LOG_DEBUG  LOG_DEBUG_INLINE
#else
#ifdef  LOG_ENABLE_ERROR
#define LOG_FATAL  LOG_FATAL_INLINE
#define LOG_ERROR  LOG_ERROR_INLINE
#define LOG_DEBUG  
#else
#ifdef  LOG_ENABLE_FATAL
#define LOG_FATAL  LOG_FATAL_INLINE
#define LOG_ERROR  
#define LOG_DEBUG        
#else
#define FATAL  
#define ERROR  
#define DEBUG        
#endif
#endif
#endif

#endif